package com.bcinfo.iccard.busi.handler;


import org.apache.commons.lang3.exception.ExceptionUtils;

import com.bcinfo.iccard.busi.AbstractBusinessHandler;
import com.bcinfo.iccard.enums.Constants;
import com.bcinfo.iccard.exception.IccardException;
import com.bcinfo.iccard.model.RechargeablecardExpensiveRecordTemp;
import com.bcinfo.iccard.util.DateTimeUtils;
import com.solab.iso8583.IsoMessage;
import com.solab.iso8583.IsoType;
/**
 * 消费撤销
 * @author wangguangchun
 *
 */
public class RevocationHandler extends AbstractBusinessHandler {

	@Override
	public void handleBusiness(IsoMessage req, IsoMessage resp)
			throws Exception {
		// TODO Auto-generated method stub
	    if(req==null ||resp==null){
            throw new IccardException("iso message req or resp is null,discard this request!");
        }
        if(_logger.isDebugEnabled()){
            _logger.debug("RevocationHandler.handleBusiness:"+req.toString());
        }
        RechargeablecardExpensiveRecordTemp temp=null;
        resp.copyFieldsFrom(req,3,11,24,37,41,42);
        resp.setValue(12, DateTimeUtils.getHourMinuteSec(), IsoType.NUMERIC, 6);
        resp.setValue(13, DateTimeUtils.getYearMonthDay(), IsoType.NUMERIC, 8);
        try
        {
            if(req.hasField(37) && req.getField(37).getValue()!=null){
                String serialNum=(String)req.getField(37).getValue();
                temp=rechargeCardTradeService.loadBySerialNum(serialNum);
                if(temp==null){
                    resp.setValue(39,Constants.ResponeCode.unableToLocateRecord.value, IsoType.ALPHA, 2);
                }else{
                    //交易撤销
                    rechargeCardTradeService.revocate(temp);
                    resp.setValue(39,Constants.ResponeCode.approval.value,IsoType.ALPHA,2);
                }
            }else{
                resp.setValue(39,Constants.ResponeCode.unableToLocateRecord.value, IsoType.ALPHA, 2);
            }
            
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            resp.setValue(39,Constants.ResponeCode.timeOut.value,IsoType.ALPHA,2);
        }finally{
            
        }
	}
	/* (non-Javadoc)
	 * Description: <br/> 
	 *
	 * @param req
	 * @param macKey
	 * @return 
	 * @see com.bcinfo.iccard.busi.AbstractBusinessHandler#authenticate(com.solab.iso8583.IsoMessage, java.lang.String)
	 */
	@Override
	protected boolean authenticate(IsoMessage req, String macKey)
	{
	    //测试,不校验mac
	    return true;
	}

}
